﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Enums;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Model.Param.TeUserManager;
using YiSha.Entity.OrganizationManage;

namespace YiSha.Service.TeUserManager
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-11 16:44
    /// 描 述：服务类
    /// </summary>
    public class TeUserService :  RepositoryFactory
    {
        #region 获取数据
        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <returns>返回所有列表信息</returns>
        public async Task<TData<List<UserEntity>>> GetList(TeUserListParam param)
        {
            var obj = new TData<List<UserEntity>>();
            var expression = ListFilter(param);
            //查询数据
            var list =  SqlSugarAdapters.GetList(expression);
            obj.Data = list;
            return obj;
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <returns>返回分页列表信息</returns>
        public async Task<TData<List<UserEntity>>> GetPageList(TeUserListParam param, Pagination pagination)
        {
            var obj = new TData<List<UserEntity>>();
            var expression = ListFilter(param);
            //查询数据
            var list=  SqlSugarAdapters.GetPager(expression, pagination);
            //获取数据及总条数
            obj.Total = list.Item1;
            obj.Data = list.Item2;
            return obj;
        }

        /// <summary>
        /// 获取数据详情
        /// </summary>
        /// <returns>返回数据详情</returns>
        public async Task<TData<UserEntity>> GetEntity(string id)
        {
            var entity= SqlSugarAdapters.GetEntity<UserEntity>(s=>s.Id==id);
            return new TData<UserEntity> { Data = entity };
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        public async Task<TData> SaveForm(UserEntity entity)
        {
            var obj = new TData();
            try
            {
                if (entity.Id.IsEmpty())
                {
                    await entity.Create();
                    SqlSugarAdapters.Insert(entity);
                }
                else
                {
                    await entity.Modify();
                    SqlSugarAdapters.Update(entity);
                }
            }
            catch (Exception ex)
            {
                obj.Msg = ex.Message;
                obj.MsgCode = MsgCode.Fail;
            }
            return obj;
        }

        /// <summary>
        /// 删除数据（物理删除）
        /// </summary>
        /// <returns></returns>
        public async Task<TData> DeleteForm(List<string> ids)
        {
            var obj = new TData();
            try
            {
                SqlSugarAdapters.Delete<UserEntity>(d => ids.Contains(d.Id));
            }
            catch (Exception ex)
            {
                obj.Msg = ex.Message;
                obj.MsgCode = MsgCode.Fail;
            }
            return obj;
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 查询表达式
        /// </summary>
        /// <returns></returns>
        private Expression<Func<UserEntity, bool>> ListFilter(TeUserListParam param)
        {
            var expression = ExtLinq.True<UserEntity>();
            if (param != null)
            {
            }
            return expression;
        }
        #endregion
    }
}
